package com.example.hasee.zhcd.Activities.ChuanWu;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Adapters.NearListAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static com.example.hasee.zhcd.R.id.fzygs;

/**
 * Created by hasee on 2018/5/31.
 */

public class NearPortActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private ListView near_port_list;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private NearListAdapter adapter;

    @Override
    public int contentViewId() {
        return R.layout.activity_near_port;
    }

    @Override
    public void set() {
        title.setText("靠泊船舶");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        getKBList();
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        near_port_list = (ListView) findViewById(R.id.near_port_list);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(NearPortActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHCW_kbcbList":
                ProgressDialogUtils.closeProgressDialog();
                Log.e("ZHCW_kbcbList", msg.getMsg());
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fstatus", data.getJSONObject(i).getString("fstatus"));
                            map.put("fberth", data.getJSONObject(i).getString("fberth"));
                            map.put("fshipcode", data.getJSONObject(i).getString("fshipcode"));
                            map.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                            map.put("fzd", data.getJSONObject(i).getString("fzd"));
                            map.put("fwbsx", data.getJSONObject(i).getString("fwbsx"));
                            map.put("fwtf", data.getJSONObject(i).getString("fwtf"));
                            map.put("fsyn", data.getJSONObject(i).getString("fsyn"));
                            map.put("fzygs", data.getJSONObject(i).getString("fzygs"));
                            map.put("fkbsj", data.getJSONObject(i).getString("fkbsj"));
                            list.add(map);
                        }
                        adapter = new NearListAdapter(NearPortActivity.this, list);
                        near_port_list.setAdapter(adapter);
                    } else {
                        Toast.makeText(NearPortActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getKBList() {
        ProgressDialogUtils.createProgressDialog(NearPortActivity.this, "正在获取数据...");
        HashMap<String, String> map = new HashMap<>();
        httpUtils.get("ZHCW_kbcbList", map, "ZHCW_kbcbList");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                break;
        }
    }
}
